"use strict";

exports.__esModule = true;
exports.isPhoneWeixin = exports.isPhoneSize = exports.getScreenWidth = exports.__webStorageCache = exports.browser = exports.Error = exports.logger = exports.debug = exports.validate = exports.cast = exports.utils = undefined;
exports.setClipboardData = setClipboardData;
exports.setStorageSync = setStorageSync;
exports.getStorageSync = getStorageSync;
exports.removeStorageSync = removeStorageSync;
exports.updateStorageSync = updateStorageSync;
exports.clearStorageSync = clearStorageSync;

require("./global");

var _error = require("./error");

var _node = require("./node");

var node = _interopRequireWildcard(_node);

var _browser2 = require("./browser");

var _browser = _interopRequireWildcard(_browser2);

var _webCacheStorage = require("./web-cache-storage");

var _webCacheStorage2 = _interopRequireDefault(_webCacheStorage);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// { utils, cast, validate, debug, logger }


var _utils = node.utils;
_utils.run = function (expression) {
    var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window;

    return Function.apply(Function, ['context', "with(context) { return " + expression + " }"])(context, expression);
};
var utils = exports.utils = _utils;

var cast = exports.cast = node.cast;
var validate = exports.validate = node.validate;
var debug = exports.debug = node.debug;
var logger = exports.logger = node.logger;
var Error = exports.Error = node.Error;

var browser = exports.browser = _browser;

function copyTextToClipboard(text) {
    var ret = false;
    var textArea = document.createElement("textarea");

    //
    // *** This styling is an extra step which is likely not required. ***
    //
    // Why is it here? To ensure:
    // 1. the element is able to have focus and selection.
    // 2. if element was to flash render it has minimal visual impact.
    // 3. less flakyness with selection and copying which **might** occur if
    //    the textarea element is not visible.
    //
    // The likelihood is the element won't even render, not even a flash,
    // so some of these are just precautions. However in IE the element
    // is visible whilst the popup box asking the user for permission for
    // the web page to copy to the clipboard.
    //

    // Place in top-left corner of screen regardless of scroll position.
    textArea.style.position = 'fixed';
    textArea.style.top = 0;
    textArea.style.left = 0;

    // Ensure it has a small width and height. Setting to 1px / 1em
    // doesn't work as this gives a negative w/h on some browsers.
    textArea.style.width = '2em';
    textArea.style.height = '2em';

    // We don't need padding, reducing the size if it does flash render.
    textArea.style.padding = 0;

    // Clean up any borders.
    textArea.style.border = 'none';
    textArea.style.outline = 'none';
    textArea.style.boxShadow = 'none';

    // Avoid flash of white box if rendered for any reason.
    textArea.style.background = 'transparent';

    textArea.value = text;

    document.body.appendChild(textArea);

    textArea.select();

    try {
        ret = document.execCommand('copy');
        // var msg = successful ? 'successful' : 'unsuccessful';
        // console.log('Copying text command was ' + msg);
    } catch (err) {
        // console.log('Oops, unable to copy');
    }

    document.body.removeChild(textArea);
    return ret;
}

var ClipboardDataError = function (_ExtendableError) {
    _inherits(ClipboardDataError, _ExtendableError);

    function ClipboardDataError() {
        _classCallCheck(this, ClipboardDataError);

        return _possibleConstructorReturn(this, _ExtendableError.call(this, "set ClipboardData error"));
    }

    return ClipboardDataError;
}(_error.ExtendableError);

/**
 * wx
 * Google Chrome 44, Firefox 42.0a1 and IE 11.0.8600.17814.
 * ali (*)
 *
 * @throws ClipboardDataError
 * @param data
 * @return {Promise<any>}
 */


function setClipboardData(data) {
    var error = new ClipboardDataError();
    return new Promise(function (resolve, reject) {
        var isSuccess = copyTextToClipboard(data);
        if (isSuccess) {
            resolve();
        } else {
            reject(error);
        }
    });
}

var __webStorageCache = exports.__webStorageCache = new _webCacheStorage2["default"]();
window.__webStorageCache = __webStorageCache;

/** 
 * 得到屏幕的大小
 */
var getScreenWidth = exports.getScreenWidth = function getScreenWidth() {
    return screen.width > 0 ? screen.width : 0;
};

var isPhoneSize = exports.isPhoneSize = function isPhoneSize() {
    var maxWidth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 768;

    return getScreenWidth() < maxWidth && this.browser.detectMediaMacthes('(max-device-width: 769px)');
};

var isPhoneWeixin = exports.isPhoneWeixin = function isPhoneWeixin() {
    return isPhoneSize() && this.browser.isLikeWeixin();
};

/**
 * 设置storage
 * 
 * @param {String} key 
 * @param {Any} data 
 * @param {Object} options 
 */
function setStorageSync(key, data, options) {
    if (options && options.expire && options.expire instanceof Date) {
        options.exp = options.expire.valueOf() - new Date().valueOf();
    }
    return __webStorageCache.set(key, data, options);
}

/**
 * 获得storage中某个key的值
 * 
 * @param {String} key 
 */
function getStorageSync(key) {
    return __webStorageCache.get(key);
}

/**
 * 删除某个值
 * 
 * @param {String} key 
 */
function removeStorageSync(key) {
    return __webStorageCache["delete"](key);
}

/**
 * 更新某个值
 * 
 * @param {*} key 
 * @param {*} value 
 * @param {*} options 
 * 
 * @returns {Boolean} isUpdated 
 */
function updateStorageSync(key, value, options) {
    return __webStorageCache.replace(key, value, options);
}

/** 
 * 清空storage  
 */
function clearStorageSync() {
    return __webStorageCache.clear();
}